package com.sbl.bajw.pc.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.QFQZModel;
import com.sbl.bajw.pc.service.QFQZModelService;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/QFQZModel")
public class QFQZModelAction {
	
	@Resource
	private QFQZModelService service;
	
	@RequestMapping(value = "/getPersonByCons")
	public @ResponseBody String getPersonByCons(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String team = request.getParameter("team");
		String unitName = request.getParameter("unit_name");
		String fullid = request.getParameter("fullid");
		String zd = request.getParameter("zd");
		String app = request.getParameter("app");
		HashMap<String, Object> map = new HashMap<String, Object>(); 
		if(unitName != null && !"".equals(unitName)){
			map.put("unitName", "%" + unitName + "%");
		}
		if(team != null && !"".equals(team)){
			map.put("team", team);
		}
		if(fullid != null && !"".equals(fullid)){
			map.put("fullid", fullid + "%");
		}
		if(zd != null && !"".equals(zd)){
			map.put("zd", zd);
		}
		if(app != null && !"".equals(app)){
			map.put("app", app);
		}
		System.out.println(map.get("zd") + "," + map.get("app"));
		String json = service.getPersonByCons(map);
		return json;
	}
	
	@RequestMapping(value = "/details")
	public ModelAndView details(HttpServletResponse response, HttpServletRequest request){
		String id = request.getParameter("id");
		QFQZModel model = service.getModelById(id);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		if(model.getComeDate() != null ){
			String data1 = sdf.format(model.getComeDate());
			request.setAttribute("comData", data1);
		}
		if(model.getEntryDate() != null){
			String data2 = sdf.format(model.getEntryDate());
			request.setAttribute("enterDate", data2);
		}
		request.setAttribute("model", model);
		ModelAndView mav = new ModelAndView("/view/grid/policePosition");
		return mav;
	}
	
	@RequestMapping(value = "/getCountByOrg")
	public @ResponseBody String getCountByOrg(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String orgcode = request.getParameter("fullid");
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("orgcode", orgcode + "%");
		List<Map<String, String>> list = service.getCountByType(map);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value = "/getSubCount")
	public @ResponseBody String getSubCount(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String orgcode = request.getParameter("fullid");
		String team = request.getParameter("team");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("orgcode", orgcode + "%");
		parm.put("team", team);
		List<Map<String, Object>> list = service.getSubCount(parm);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value = "getModelByConditions")
	public @ResponseBody String getModelByConditions(HttpServletRequest request, HttpServletResponse response){
		String xm = request.getParameter("xm");
		String sex = request.getParameter("sex");
		String card = request.getParameter("card");
		String team = request.getParameter("team");
		String phone = request.getParameter("phone");
		String jw = request.getParameter("jw");
		String zd = request.getParameter("zd");
		String app = request.getParameter("app");
		String fullid = request.getParameter("fullid");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(xm != null && "".equals(xm)){
			parm.put("xm", "%" + xm + "%");
		}
		parm.put("sex", sex);
		if(card != null && "".equals(card)){
			parm.put("card", "%" + card + "%");
		}
		if(team != null && "".equals(team)){
			parm.put("team", team);
		}
		System.out.println(parm.get("xm") + "," + parm.get("sex"));
		if(phone != null && "".equals(phone)){
			parm.put("phone", "%" + phone + "%");
		}
		if(jw != null && "".equals(jw)){
			parm.put("jw", "%" + jw + "%");
		}
		parm.put("zd", zd);
		parm.put("app", app);
		parm.put("fullid", fullid + "%");

		System.out.println(xm);
		String json = service.getModelByConditions(parm);
		return json;
	}
	
	@RequestMapping(value = "/getQFQZUnit")
	public @ResponseBody String getQFQZUnit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String org = RequestUnit.getString("org");
		System.out.println(org);
		HashMap<String, String> parm = new HashMap<String, String>();
		if(org != null && !org.equals("")){
			parm.put("org", "%" + org + "%");
		} else {
			parm.put("org", "%%");
		}
		List<HashMap<String, Object>> model = service.getQFQZUnit(parm);
		String json = JsonUtil.ConvertArrayToJson(model);
		return json;
	}
}
